function gadget:GetInfo()
	return {
		name = "Engine Handler",
		desc = "Handles everything about the engines",
		author = "SkyStar",
		date = "2010",
		license = "GNU GPL v2, or later",
		layer = 1,
		enabled = true
	}
end

if (gadgetHandler:IsSyncedCode()) then
----------------------------------
----------- SYNCED ---------------
----------------------------------

local oldTime = 0
local deadEngines = {}
local basePos = {}

----------------------------------

	function gadget:Initialize() -- Everything as soon as Gadget loads
	-- Prepare Global Framework functions --
	Gfunction = _G.functions
	end -- Initialize()

	function gadget:UnitCreated(unitID,unitDefID,unitTeam) -- Handle stuff that spawns
	-- If your not dead don't get in the way -- 
		if deadEngines[unitTeam] then
		local x,y,z = Spring.GetUnitPosition(unitID)
		local facing = Spring.GetUnitBuildFacing(unitID)
		if(facing == 0) then 
		z = z + math.random(175,225)
		x = x + math.random(-125,125)
		end		
		if(facing == 1) then 
		x = x + math.random(175,225)
		z = z + math.random(-125,125)
		end		
		if(facing == 2) then
		z = z - math.random(75,125)
		x = x + math.random(-125,125)
		end
		if(facing == 2) then
		x = x - math.random(75,125)
		z = z + math.random(-125,125)
		end
		-- Move engines a bit -- 
		Spring.SetUnitPosition(unitID, x, y, z)
		-- Remove from dead list --
		deadEngines[unitTeam] = nil
		end
	end -- UnitCreated()
	
	function gadget:UnitDestroyed(unitID, unitDefID, unitTeam, attackerID, attackerDefID, attackerTeam) -- When something dies
		-- If an engine died --
		if Gfunction.isEngine(unitID) then
		-- Determine respawn time 10 Seconds plus 2 seconds for every minute in the game --
		local time = math.floor(Spring.GetGameSeconds())
		time = 10 + (math.floor((time/60)*2))
		-- Log the death --
		deadEngines[unitTeam] = {['engine'] = unitDefID,['time'] = time}
		SendToUnsynced("respawn", unitTeam, attackerTeam, time)
		end
	end -- UnitDestroyed()

	function gadget:GameFrame(frame) -- Everything while game is not paused
	local time = math.floor(Spring.GetGameSeconds())
	-- Update only once per second --
		if(time ~= oldTime) then
		oldTime = time
		-- Go through the list of dead engines --
			for unitTeam,_ in pairs(deadEngines) do 
			-- Count down for respawn --
				if(deadEngines[unitTeam].time > 0) then
				deadEngines[unitTeam].time = deadEngines[unitTeam].time - 1
				end
			-- Respawn Engine if countdown is finished	
				if(deadEngines[unitTeam].time == 0) then
				local allyID = Gfunction.getAllyID(unitTeam)
				local x,y,z = basePos[allyID].x,basePos[allyID].y,basePos[allyID].z
				local facing = Gfunction.getFacing(tonumber(x),tonumber(z))	
				Gfunction.createUnit(deadEngines[unitTeam].engine, x, y, z, facing, unitTeam, true)
				end				
			end
		end	
	end -- GameFrame()
		
		function gadget:RecvLuaMsg(msg, playerID) -- Recieve commands from unsynced ()
		if string.find(msg,"Gfunction:") then
		local txt = msg:gsub("Gfunction:","")
		local params = Gfunction.split(txt,',')
		-- Get base position --
			if(params[1] == 'basePos') then
			local allyID = tonumber(params[5])
			local x,y,z = params[2],params[3],params[4]
			basePos[allyID] = {['x'] = x, ['y'] = y, ['z'] = z}
			end
		end
	end
end

if (not gadgetHandler:IsSyncedCode()) then	

-------------------------------
------ UN-SYNCED --------------
-------------------------------

local localTeamID = Spring.GetLocalTeamID()	
local respawnTime = 0
local oldTime = 0
local attacker = ""
LUAUI_DIRNAME = 'LuaUI/'
local fontHandler   = loadstring(VFS.LoadFile(LUAUI_DIRNAME.."modfonts.lua", VFS.ZIP_FIRST))()
local fancyFont		= LUAUI_DIRNAME.."Fonts/KOMTXT___16"

------------------------------

	function respawn(_, deadTeam, attackerTeam, time) -- Recieve death message from sync
		if(deadTeam == localTeamID) then
			if(not attackerTeam) then
			attacker = "yourself"
			respawnTime = time + 1
			return
			end
		local _, player = Spring.GetTeamInfo(attackerTeam)
		attacker = Spring.GetPlayerInfo(player) or "Unknown"
		respawnTime = time + 1
		end
	end -- respawn()
	
	function gadget:Initialize() -- Stuff to start when gadget loads
	gadgetHandler:AddSyncAction("respawn", respawn)
	end -- Initialize()
	
	function gadget:DrawScreen()
	local time = math.floor(Spring.GetGameSeconds())	
		if(respawnTime ~= 0 and time ~= oldTime) then		
		oldTime = time
		respawnTime = respawnTime - 1
		end
		if(respawnTime ~= 0) then
		local vsx, vsy = gl.GetViewSizes()
		local posx = vsx * 0.5
		local posy = vsy * 0.5				
		fontHandler.UseFont(fancyFont)
		gl.PushMatrix()
		
		gl.Color(1.0,0,0,1.0)		
		gl.PushMatrix()
		gl.Translate(posx,posy,0)
		gl.Scale(3, 3, 3)
		fontHandler.DrawCentered("You have been killed by: "..attacker, 0,0)
		gl.PopMatrix()
		gl.Translate(posx,posy-50,0)
		gl.PushMatrix()
		gl.Scale(2.5, 2.5, 2.5)
		
		fontHandler.DrawCentered("Respawning in: "..respawnTime, 0,0)
		-- gl.PopMatrix()
		end		
	end -- DrawScreen()
end